package com.px.user.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.px.common.util.JsonUtil;
import com.px.user.model.ActionModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.sql.Timestamp;
import java.util.Arrays;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "course")
@ApiModel(value = "课程对象", description = "课程字段")
@org.hibernate.annotations.Table(appliesTo = "course", comment = "课程表")
public class Course {

    @Id
    @ApiModelProperty(value = "主键", example = "4")
    @NotNull(groups = {ActionModel.Update.class})
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Null(groups = {ActionModel.Insert.class}, message = "删除id字段")
    @Column(unique = true, nullable = false, columnDefinition = "int comment '主键'")
    private Integer id;

    @Column(columnDefinition = "bigint comment '创建人ID'")
    @ApiModelProperty(value = "创建者ID", example = "1", hidden = true)
    private Long creatorId;

    @NotNull(groups = {ActionModel.Insert.class})
    @ApiModelProperty(value = "讲师ID", example = "1")
    @Column(columnDefinition = "bigint comment '讲师ID'")
    private Long teacherId;

    @NotNull(groups = {ActionModel.Insert.class})
    @ApiModelProperty(value = "房间IDs", example = "1,2")
    @Column(columnDefinition = "json comment '房间IDs'")
    private String roomIds;

    @NotNull(groups = {ActionModel.Insert.class})
    @ApiModelProperty(value = "房间名字", example = "房间名字")
    @Column(columnDefinition = "varchar(512) comment '房间名字'")
    private String roomName;

    @ApiModelProperty(value = "房间简介", example = "房间简介demo")
    @Column(columnDefinition = "varchar(1024) comment '房间简介'")
    private String roomIntro;

    @ApiModelProperty(value = "推流地址")
    @Column(columnDefinition = "varchar(512) comment '推流地址'")
    private String pushSource;

    @ApiModelProperty(value = "拉流地址")
    @Column(columnDefinition = "varchar(512) comment '拉流地址'")
    private String fetchSource;

    @ApiModelProperty(value = "开始时间", example = "1566880452935")
    @Column(columnDefinition = "timestamp null comment '开始时间'")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Timestamp startTime;

    @ApiModelProperty(value = "结束时间", example = "1566966852000")
    @Column(columnDefinition = "timestamp null comment '结束时间'")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Timestamp endTime;

    @Generated(GenerationTime.INSERT)
    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP comment '创建时间'", insertable = false, updatable = false)
    private Timestamp createTime;

    @Generated(GenerationTime.INSERT)
    @ApiModelProperty(value = "更新时间", hidden = true)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间'", insertable = false, updatable = false)
    private Timestamp updateTime;

    public String roomIds2json(String roomIds) {
        return JsonUtil.toJson(Arrays.asList(roomIds.split("\\s*,\\s*")));
    }

    public String json2roomIds(String roomIds) {
        return roomIds.replace("[", "")
                .replace("]", "")
                .replace("\"", "")
                .replace(" ", "");
    }

    /**
     * 扩展字段
     */
    @Transient
    @ApiModelProperty(value = "讲师昵称", hidden = true)
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String teacherNickname;

    /**
     * 扩展查询字段
     */
    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询开始时间", hidden = true)
    private Timestamp qstart;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询结束时间", hidden = true)
    private Timestamp qend;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询房间ID", hidden = true)
    private Long qroomId;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询排序字段", hidden = true)
    private String orderField;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询开始索引", hidden = true)
    private Integer startIndex;

    @Transient
    @JsonIgnore
    @ApiModelProperty(value = "查询返回数量", hidden = true)
    private Integer limit;
}
